import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import { createPinia } from "pinia";
import ElementPlus from "element-plus";
import "element-plus/dist/index.css";
import "element-plus/theme-chalk/dark/css-vars.css";
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
import zhCn from "element-plus/es/locale/lang/zh-cn";

// 样式文件
import "./styles/index.scss";

// 工具函数
import { setupGlobalProperties } from "./utils/global";
import { setupDirectives } from "./directives";
import { setupErrorHandler } from "./utils/errorHandler";

// 进度条
import NProgress from "nprogress";
import "nprogress/nprogress.css";

// 配置进度条
NProgress.configure({
  showSpinner: false,
  trickleSpeed: 200,
  minimum: 0.3,
});

async function bootstrap() {
  // 创建应用实例
  const app = createApp(App);

  // 创建Pinia状态管理
  const pinia = createPinia();
  app.use(pinia);

  // 配置Element Plus
  app.use(ElementPlus, {
    locale: zhCn,
    size: "default",
  });

  // 注册Element Plus图标
  for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
  }

  // 设置全局属性
  setupGlobalProperties(app);

  // 设置自定义指令
  setupDirectives(app);

  // 设置错误处理
  setupErrorHandler(app);

  // 使用路由
  app.use(router);

  // 挂载应用
  app.mount("#app");

  // 应用启动成功
  // 🎉 PersonaLink 应用启动成功!
}

// 启动应用
bootstrap().catch(() => {
  // 应用启动失败
  // ❌ 应用启动失败
});
